<template>
  <div class="top">
    <el-form :model="ruleForm" ref="ruleForm" label-position="center" label-width="100px" class="demo-ruleForm">

      <label id="UserPermission">新用户赋予权限</label>


      <div class="ddiv">
        <el-form-item label="用户id" prop="userId" label-width="100px">
          <!--        <el-input v-model="ruleForm.userId"></el-input>-->
          <el-select class="aa" size="mini" v-model="ruleForm.userId">
            <el-option v-for="item in info"
                       :key="item.user.userId"
                       :label="item.user.username"
                       :value="item.user.userId">
            </el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="权限id" prop="roleId" label-width="100px">
          <el-select size="mini" v-model="ruleForm.roleId">
            <el-option v-for="item in displayOptions"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </div>

    </el-form>
  </div>

</template>

<script>
export default {
  name: "permission-manager",
  data() {
    return {
      ruleForm:{
        userId: '',
        roleId: ''
      },
      userInfo:[],
      pageNum:1,
      pageSize:1000,
      total:100,
      //是否显示(可以由后台返回)
      displayOptions:[
        {
          value:"1",
          label:"学生"
        },
        {
          value:"2",
          label:"教师"
        },
        {
          value:"3",
          label:"教务"
        },
      ],
    }
  },
  methods:{
    getList(){
      this.$axios.get("/user/getAll?pageNum=" + 1 + '&pageSize=' + 1000 + '&roleId=' + 0,
          {
            headers: {
              "Authorization": this.$store.getters.getToken
            }
          }
      ).then(response => {
        // const list = response.data.data;
        this.userInfo = '';
        this.userInfo = response.data.data.list;
        this.total = response.data.data.total
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.post("/user-role/permission", this.ruleForm,
              {
                headers: {
                  "Authorization": this.$store.getters.getToken
                }
              }).then(res => {
            this.$message({
              message: res.data.message,
              type: "success"
            });
            this.getList();
            this.resetForm('ruleForm');
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
  created() {
    this.getList()
  },
  computed:{
    info() {
      this.getList();
      return this.userInfo;
    }
  },
  watch: {
    info: {
      immediate: true,    //初始化时让handler调用一下
      //handler什么时候调用：当isHot发生变化时
      handler(newValue, oldValue){
        console.log(newValue,oldValue)
      }
    }
  }
}
</script>

<style scoped>

.top{
  align-content: center;
  text-align: center;
}

.demo-ruleForm{
  width: 100%;
  align-content: center;
  text-align: center;
}

#UserPermission{
  font-size: 36px;
}

.ddiv{
  text-align: center;
  align-content: center;
  width: 300px;
}

</style>